package org.sam.swing.table.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;

import org.jdesktop.swingx.JXTable;
import org.sam.swing.resource.ResourceLoader;
import org.sam.swing.table.JSTableModel;

/**
 * 追加操作对象
 * 
 * @author sam
 *
 */
public class JSTableDownAction extends JSTableBaseAction {

	private static final long serialVersionUID = -9188494703414826780L;

	public JSTableDownAction(JXTable table) {
		super(table);
		this.putValue(AbstractAction.NAME, "");
		this.putValue(AbstractAction.SMALL_ICON,
				new ImageIcon(ResourceLoader.getResource(ResourceLoader.IMAGE_DOWN)));
		this.putValue(SHORT_DESCRIPTION, "向下移动");
	}

	/**
	 * 追加数据
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			JSTableModel<?> model = (JSTableModel<?>) this.getTable().getModel();
			// 当前选中行
			int iRow = table.getSelectedRow();
			if (iRow >= table.getRowCount() - 1)
				return;

			// 调换2个行的index数据
			int iNext = table.convertRowIndexToModel(iRow + 1);
			iRow = table.convertRowIndexToModel(iRow);
			model.moveRow(iRow, iNext);
			// 交换值
			//下一行高亮
			table.changeSelection(iNext, 0, false, false);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
